import * as CONSTANT from './constant'
import {EOL} from 'os'

export function parseSgf(sgf_data) {
    // W为白 B为黑，后面的字母是1-19的坐标
    // B[dp];
    sgf_data = sgf_data.split(EOL);
    let data = ''
    for (let i = 0; i < sgf_data.length; i++) {
        data += sgf_data[i]
    }

    let arr = data.split(';')
    let res = [];
    for (let i = 0; i < arr.length; i++) {
        let step = arr[i];

        let color = CONSTANT.COLOR_DICT[step[0]];
        let x = CONSTANT.ALPHABET[step[2]];

        let y = CONSTANT.ALPHABET[step[3]];
        // 20211004 不去校验长度了 因为考虑各家的棋谱有些微妙差异 很容易丢步数 只要能匹配到值就默认OK
        if (x && y && color) {
            res.push({
                "color": color,
                "x": x,
                "y": y
            });
        }


    }
    return res
}

export function getEmptyGoban() {
    return [
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    ]
}

export function getEmptyString() {
    return {
        black: {},
        white: {},
        //棋串编号
        num: 19,
    }
}

export function get4Direction(x, y) {
    let up = {
        x,
        y: y + 1,
    };
    let down = {
        x,
        y: y - 1,
    };
    let left = {
        x: x - 1,
        y,
    };
    let right = {
        x: x + 1,
        y,
    };
    return {up, down, left, right};
}
